﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.SessionState;
namespace wxx.wxxweb
{
    /// <summary>
    /// mobile 的摘要说明
    /// </summary>
    public class mobile : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request.QueryString["init"] != null)
            {
                string str = readlist("", context,"");
                if (str == "") { context.Response.Write("false"); }
                else
                {
                   
                    context.Response.Write(str);
                }
            }
          
            if (context.Request.QueryString["search"] != null)
            {
               
                    String BookName = context.Request.QueryString["cplist"].ToString();
                    String type = context.Request.QueryString["type"].ToString();
                    string str = readlist(BookName, context,type);
                    if (str == "") { context.Response.Write("false"); }
                    else
                    {
                        context.Response.Write(str);
                    }
                
        
            }
            if (context.Request.QueryString["mbedit"] != null)
            {
                String str = readedit(context.Request.QueryString["mbedit"].ToString(), context);
                context.Response.Write(str);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private string readlist(string cpmingcheng, HttpContext context,string type)
        {
            wxx.BLL.t_bookinfo bibll = new wxx.BLL.t_bookinfo();
            wxx.Model.t_bookinfo bimodel = new wxx.Model.t_bookinfo();
            string strif = " order by id desc";
            DataSet ds1 = new DataSet();
            if (cpmingcheng != ""&&type=="书名")//如果搜索框不空
                ds1 = bibll.GetList("BookName like'%" + cpmingcheng + "%'");
            // strif = " where BookName like '%" + cpmingcheng + "%' order by id desc";
            else if (cpmingcheng != "" && type == "作者") {
                ds1 = bibll.GetList("Writer like'%" + cpmingcheng + "%'");
            }
            else//搜索框为空就出现全部数据
            {
                ds1 = bibll.GetAllList();
            }
            string strret = "";
          
            if (ds1.Tables[0].Rows.Count > 0)
            {
                 strret = "<table class='table' style='margin-top:10px'>\r\n";

                strret += "<thead><tr><th >书籍名称</th><th >书籍类型</th><th scope='col'>书籍编号</th><th >作者</th><th>出版社</th><th>出版时间</th><th >价格</th><th >可借数量</th><th >操作人</th></tr></thead><tbody>\r\n";

                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    if (i % 2 == 0)
                        strret += "<tr> <td class='success'>" + ds1.Tables[0].Rows[i]["BookName"].ToString() + "</td> <td>" + ds1.Tables[0].Rows[i]["BookType"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["ISBN"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["Writer"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["PublishName"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["PublishDate"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["Jiage"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["Beizhu"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["Operation"].ToString() + "</td><td><button type='submit' class='btn btn-default' onclick=\"order('" + ds1.Tables[0].Rows[i]["id"].ToString() + "');\">预约</button></td> </tr>\r\n";
                    else
                        strret += "<tr class='warning'> <td>" + ds1.Tables[0].Rows[i]["BookName"].ToString() + "</td> <td>" + ds1.Tables[0].Rows[i]["BookType"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["ISBN"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["Writer"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["PublishName"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["PublishDate"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["Jiage"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["Beizhu"].ToString() + "</td><td>" + ds1.Tables[0].Rows[i]["Operation"].ToString() + "</td> <td><button type='submit' class='btn btn-default' onclick=\"order('" + ds1.Tables[0].Rows[i]["id"].ToString() + "'); \">预约</button></td> </tr>\r\n";
                }
                strret += "</tbody></table>\r\n";
                return strret;
            }
            else
            {
               
                return strret;
            }
        }
        private String readedit(string myid, HttpContext context)
        {

            wxx.BLL.t_bookinfo bibll=new wxx.BLL.t_bookinfo();
            wxx.Model.t_bookinfo bimodel= new wxx.Model.t_bookinfo();
            string strif = " order by id desc";
            if (myid != "")
                strif = " where id = " + myid + " order by id desc";

            DataSet ds1 = new DataSet();
           
            bimodel =bibll.GetModel(int.Parse(myid));//获取书目信息表中的记录
            String Num = bimodel.Beizhu;
            String ISBN=bimodel.ISBN;
            String BookName=bimodel.BookName;
            if (int.Parse(Num) > 0) { return "false"; }//库存充足无需预约
            else//预约记录中多一条记录
            {
                //查看预约中是否有记录是相同的若相同则说明无需重复预约
                wxx.Model.t_order omodel = new wxx.Model.t_order();
                wxx.BLL.t_order obll = new wxx.BLL.t_order();
                String ReaderId = context.Session["user"].ToString();
                DataSet ods = obll.GetList("ReaderId='" + ReaderId + "'" + "and ISBN='" + ISBN + "'" + "and State=" + "'已预约'");
                DataSet allds = obll.GetList("ReaderId='" + ReaderId + "'" + "and State=" + "'已预约'");
                if (allds.Tables[0].Rows.Count >= 5) { return "toomore"; }//您已借阅5本书
                else
                {
                    if (ods.Tables[0].Rows.Count > 0) { return "yiyuyue"; }//记录中存在一样的说明已预约
                    else
                    {

                        omodel.ReaderId = ReaderId;
                        omodel.ISBN = ISBN;
                        omodel.BookName = BookName;
                        omodel.OrderTime = DateTime.Now;
                        omodel.State = "已预约";

                        obll.Add1(omodel);
                        return "true";
                    }
                }
            }

        }
    }
}